<template>
  <div>
    <el-dialog title="绑定能耗设备" :visible.sync="addEditIsDialog_c" :close-on-click-modal="false" @close="resetForm('formName')" width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm" :class="{ 'form-rules-show': seeType }">
        <el-row>
          <el-col :span="24">
            <el-form-item label="能耗设备：" prop="energyConsumptionIdArr">
              <el-select v-model="ruleForm.energyConsumptionIdArr" placeholder="请选择能耗设备" multiple collapse-tags clearable>
                <el-option v-for="item in energyConsumptionArray" :key="item.id+''" :label="item.name" :value="item.id+''"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button type="primary" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { getDeviceInfo, bindEnergyConsumptionDevice } from '@/api/device'
import { getEnergyConsumptionDevices } from '@/api/energyConsumption'

export default {
  components: {},
  props: {
    addEditId: {
      type: String,
      default: ''
    },
    addEditIsDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: '',
        energyConsumptionIds: '',
        energyConsumptionIdArr: []
      },
      rules: {
        // 表单规则
        energyConsumptionIdArr: [{ required: true, message: '请选择能耗设备', trigger: 'change' }]
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      energyConsumptionArray: [] // 能耗设备集合
    }
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val
      if (this.addEditIsDialog_c) {
        // 添加
        this.ruleForm.id = this.addEditId

        // 详情
        this.getDetail()
      }
    }
  },
  methods: {
    // 详情
    getDetail() {
      getDeviceInfo({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          res.data.energyConsumptionIdArr = res.data.energyConsumptionIds != undefined ? res.data.energyConsumptionIds.split(',') : []

          this.ruleForm = res.data

          // 获取能耗设备列表
          this.getEnergyConsumptionDeviceList()
        }
      })
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.energyConsumptionIds = this.ruleForm.energyConsumptionIdArr.toString()
          bindEnergyConsumptionDevice(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg
              })

              // 关闭弹窗
              this.$parent.closeAddEdit()
              // 刷新列表
              this.$parent.onSubmit()
            }
          })
        }
      })
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.$parent.closeAddEdit()
    },
    // 获取能耗设备列表
    getEnergyConsumptionDeviceList() {
      getEnergyConsumptionDevices({ id: this.ruleForm.examinationRoomId }).then((res) => {
        if (res.code == 200) {
          this.energyConsumptionArray = res.data
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-input-number {
    width: 100%;
  }

  .el-cascader {
    width: 100%;
  }
}
</style>
